package api

import (
	"errors"
	"github.com/gin-gonic/gin"
	"go-caipu/pkg/api/helper"
	"go-caipu/pkg/constant"
	"go-caipu/pkg/services/tools"
	"go-caipu/pkg/services/tools/models"
	"strconv"
)

/**
说明： 代码生成器
参考代码： https://gitee.com/go-admin-team/go-admin/tree/master
*/

func (hs *HTTPServer) GetSysTablePage(c *gin.Context) {
	cmd := tools.SysTableSearch{}
	if err := helper.BindAndValidate(c, &cmd); err != nil {
		return
	}

	result, count, err := hs.toolService.GetSysTablesPage(cmd)
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}

	helper.SuccessWithData(c, helper.Page{CurrentPage: cmd.PageIndex, PageSize: cmd.PageSize, List: result, Total: count})
}
func (hs *HTTPServer) GetSysTable(c *gin.Context) {
	id, err := strconv.Atoi(c.Param("id"))
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}

	result, err := hs.toolService.GetSysTable(id)
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	mp := make(map[string]interface{})
	mp["list"] = result.Columns
	mp["info"] = result
	helper.SuccessWithData(c, mp)

}

func (hs *HTTPServer) GetSysTableByTableName(c *gin.Context) {
	tableName := c.Param("tableName")
	if tableName == "" {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, errors.New("tablename is nil"))
		return
	}
	result, err := hs.toolService.GetSysTableByTableName(tableName)
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}

	helper.SuccessWithData(c, result)

}
func (hs *HTTPServer) InsertSysTables(c *gin.Context) {
	cmd := tools.InsertSysTableCmd{}
	if err := helper.BindAndValidate(c, &cmd); err != nil {
		return
	}

	if len(cmd.Tables) == 0 {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, errors.New("tables is nil"))
		return
	}
	err := hs.toolService.InsertSysTables(cmd)
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	helper.SuccessWithOutData(c)
}

func (hs *HTTPServer) UpdateSysTable(c *gin.Context) {
	cmd := models.SysTables{}
	if err := helper.BindAndValidate(c, &cmd); err != nil {
		return
	}

	result, err := hs.toolService.UpdateSysTable(cmd)
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	helper.SuccessWithData(c, result)

}
func (hs *HTTPServer) RemoveSysTable(c *gin.Context) {
	cmd := tools.RemoveToolsCommand{}
	if err := helper.BindAndValidate(c, &cmd); err != nil {
		return
	}

	_, err := hs.toolService.RemoveSysTable(cmd.Ids)
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	helper.SuccessWithOutData(c)

}

/*
	db_tables
*/

func (hs *HTTPServer) GetDBTablePage(c *gin.Context) {
	cmd := tools.SysDBTableSearch{}
	if err := helper.BindAndValidate(c, &cmd); err != nil {
		return
	}
	result, count, err := hs.toolService.GetDBTablePage(cmd.TableName, cmd.GetPageSize(), cmd.GetPageIndex())
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	helper.SuccessWithData(c, helper.Page{CurrentPage: cmd.PageIndex, PageSize: cmd.PageSize, List: result, Total: count})
}

/*
db_columns
*/
func (hs *HTTPServer) GetDBColumnPage(c *gin.Context) {
	cmd := tools.GetColumnSearch{}
	if err := helper.BindAndValidate(c, &cmd); err != nil {
		return
	}

	result, count, err := hs.toolService.GetDBColumnPage(cmd.GetPageSize(), cmd.GetPageIndex())
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}

	helper.SuccessWithData(c, helper.Page{CurrentPage: cmd.PageIndex, PageSize: cmd.PageSize, List: result, Total: count})
}
